package cn.fulong.sso.client.manager;

import cn.fulong.sso.client.constant.SSOConstants;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpSession;


/**
 * @desc: 实现会话管理部分功能, 本地会话管理
 * @author: 云树
 * @createDate: 2019/1/17 16:30
 * @version: 1.0
 */
@Slf4j
public abstract class AbstractSessionManager implements SessionManager<HttpSession> {


    @Override
    public boolean bindSessionManager(String token, HttpSession sessionStorage) {
        //session中添加token映射
        sessionStorage.setAttribute(SSOConstants.Session.TOKEN, token);
        //记录token与session的映射，用于全局回调退出
        this.addTokenSessionStorage(token, sessionStorage);
        return true;
    }


    @Override
    public boolean removeSessionManagerBySession(HttpSession session) {
        if (session != null) {
            String token = (String) session.getAttribute(SSOConstants.Session.TOKEN);
            //解除token跟本地HttpSession映射
            this.removeTokenMappingSessionByToken(token);
            try {
                session.invalidate();
            } catch (Exception e) {
                log.error("【ConcurrentHashMapSessionManager】根据session清除会话", e);
            }
        }
        return true;
    }

}
